tab.path <- "results/table/"
tab.name <- "app_tab_d01.tex"

yvars <- c("par.onl.sh")
fcontrols <- c(controls, paste0("dpost:",controls))

vars <- c( "factor(dpost)*factor(treat_10)","factor(dpost)" ,"factor(treat_10)")
outvars <- c( "factor.dpost.TRUE.factor.treat_10.TRUE","^factor.dpost.TRUE$" ,"^factor.treat_10.TRUE$")
labvars <- c("DPost X March","DPost", "March")

Re$ecmr <- Re$ecmr/100
### Controls 

KonOut <- c( "^log.min.dist.city10k$",  
  "^log.pop$" , "^log.dist.roads$", 
   "^av_age$", "^sexr$", "^single_$",  "^tfr$", 
   "^f_smam$", "^f_cel_4$", "^m_cel_4$",
   "^fmar_pr$",
   "^hc1$",  "^hc2$", "^hc3$", "^hc4$", "^hc5$",
   "^ecmr$")

KonLab <- c("Distance to City (log, km)",
    "Population (log, thousands)",
    "Distance to Road (log, km)",
    "Average Age",
    "Female Share of Population",
    "Share of Single Person Households, pct",
    "Total Fertility Rate (children per women)",
    "Age at Marriage for Women",
    "Female Celibacy Rate",
    "Male Celibacy Rate",
    "Share of Married Women Working",
    "HISCLASS 1 (High Skill Non Manual, pct)",
    "HISCLASS 2 (Lower Skill Non Manual, pct)",
    "HISCLASS 3 (High Skill Manual, pct)",
    "HISCLASS 4 (Lower Skill Manual, pct)",
    "HISCLASS 5 (Unskilled)",
    "Early Child Mortality Rate (per 100,0000)" 
    )
    
m1  <- felm(as.formula(RegFor( y = yvars[1] , x = c(vars) ,
      FE = "county" , IV="0", clust = "klust" )),
           data= Re)
m2 <- felm(as.formula(RegFor( y = yvars[1] , x = c(vars, fcontrols) ,
      FE = "county" , IV="0", clust = "klust" )),
           data= Re)
m3 <- felm(as.formula(RegFor( y = yvars[1] , x = c(vars, fcontrols) ,
      FE = "county" , IV="0", clust = "klust" )),
           data= subset(Re, year %in% c(1912, 1914)))
m4 <- felm(as.formula(RegFor( y = yvars[1] , x = c(vars, fcontrols) ,
      FE = "county" , IV="0", clust = "klust" )),
           data= subset(Re, year %in% c(1911,1912, 1914)))
m5 <- felm(as.formula(RegFor( y = yvars[1] , x = c(vars, fcontrols) ,
      FE = "county" , IV="0", clust = "klust" )),
       data= subset(Re, year %in% c(1911,1912, 1913, 1914) 
        & (pop < 20000 & (dist.roads <= 2 | treat_10 == 1))))
       
getwd()
regstoadd <- list(m1,m2, m3,m4, m5 )

MSd <- function(variable){
  x1 <- round(mean(variable, na.rm = TRUE), 2)
  x2<-  round(sd(variable, na.rm = TRUE), 2)
  return(c(x1,x2))
}

Stats <- rbind(MSd(Re[,yvars[1]]),
  MSd(Re[,yvars[1]]) ,
  MSd(Re[,yvars[1]]) ,
  MSd(Re[Re$year %in% c(1912, 1914),yvars[1]]),
  MSd(Re[Re$year %in% c(1911, 1912, 1914),yvars[1]]),
  MSd(Re[Re$year %in% c(1911, 1912, 1914) & Re$pop < 10000 ,yvars[1]])
)
####OUT
add.lines <- list(AddLines( 7, "County FE", rep(LMc("Yes"),5)),
  AddLines( 7, "Incl. 1913", c(rep(LMc("Yes"),2), c(LMc("No"), LMc("No"), LMc("Yes")))),
  AddLines( 7, "Incl. 1911", c(rep(LMc("Yes"),2), c(LMc("No"), LMc("Yes"), LMc("Yes")))),
  AddLines( 7, "Controls", c(LMc("No"),  rep(LMc("Yes"),4))),
  AddLines( 7, "Pop under 15k", c(rep(LMc("No"),4), LMc("Yes"))),
  AddLines( 7, "Within 2~km of roads ", c(rep(LMc("No"),4), LMc("Yes"))),
  AddLines( 7, "Mean dep. var." , Stats[,1] ),
  AddLines( 7, "Sd dep. var." , Stats[,2] )
 )

fileConn<-file(paste0(tab.path,tab.name))
print(fileConn)
writeLines(stargazer(regstoadd,
   float = FALSE ,
   keep= c(outvars, KonOut),
   order = c(outvars , KonOut),
   covariate.labels= c(labvars , KonLab),
   star.cutoffs = c(0.1099, 0.0599, 0.0199) ,
   digits.extra = 0,
   multicolumn=F,#
   #column.sep.width="10pt",
   dep.var.caption = paste("Share of Local Electors" ),
   dep.var.labels.include = F,
   font.size = "footnotesize",
   omit.table.layout = "n",
   align =T,
   add.lines = add.lines,
   digits = 3,
   intercept.top = T,
   intercept.bottom = F,
   keep.stat = c("rsq" , "n") ,
   omit.stat = c("res.dev","ser") ),
fileConn)
close(fileConn)

Re$ecmr <- Re$ecmr*100

rm(list=
    setdiff(ls()[sapply(ls(), function(x) any(class(get(x)) == 'data.frame'))],
    c("Re","Ind")))